﻿/* 
* 陈玉堂  WebSockt对象接口
* 是否有重新连接IsReConnect设置为true，reConnectTime为重连的间隔；是否有心跳IsKeepAlive设置true|false, KeepAliveTime为心跳间隔, KeepAliveData心跳包参数
* 初始化：WebSocktInterface({"url":"",IsReConnect:true|false,reConnectTime:20,onOpen:function(e){},onMessage:function(e){},onError:function(e){},onClose:function(e){} });
* 方法：返回0成功，返回-1失败
* 发送：WebSocktInterface("Send","msg");
* 关闭：WebSocktInterface("Close");
* 重连：WebSocktInterface("ReConnect");
* 心跳：WebSocktInterface("KeepAlive");
* 错误：WebSocktInterface("GetError",code);
*/
var WebSocktInterface = (function () {
    var wsObject = null; //连接对象
    var wsOptions = null; //当前socket的参数
    var wsInterval = null; //当前心跳定时器
    var wsTimeout = null; //当前重连定时器
    //方法
    
    var Methods = {
        Send: function (param) {
            function send() {
                wsObject.send(param);
            }
            if (wsObject.readyState == 1) {
                send();
            }
        },
        Close: function () {
            wsOptions.IsReConnect = false;
            if (!!wsObject) {
                wsObject.close();
            }
        },
        ReConnect: function () {
            var time = wsOptions.reConnectTime || 10; //默认为10秒
            wsTimeout = setTimeout(function () {
                WebSocktInterface(wsOptions);
            }, time * 1000);
        },
        KeepAlive: function () {
            var time = wsOptions.KeepAliveTime || 20; //默认为20秒
            if (!!wsInterval) {
                return;
            }
            if (!!wsOptions.IsKeepAlive && !!wsOptions.KeepAliveData) {
                wsInterval = setInterval(function () {
                    Methods["Send"](wsOptions.KeepAliveData);
                }, time * 1000);
            }
        },
        GetError: function (code) {
            var msgStr;
            switch (code) {
                case -1:
                    msgStr = "您的浏览器不支持WebSocket";
                    break;
                case 0:
                    msgStr = "WebSocket连接尚未创建";
                    break;
                case 1:
                    msgStr = "WebSocket连接已经建立";
                    break;
                case 2:
                    msgStr = "WebSocket连接正在关闭";
                    break;
                case 3:
                    msgStr = "WebSocket连接已经关闭或不可用";
                    break;
                case 4:
                    msgStr = "指定方法未定义";
                    break;
                case 5:
                    msgStr = "初始化WebSocket未定义url";
                    break;
            }
            return msgStr;
        }
    };
    return function (options, param) {
        if (options == "GetError") {
            return Methods["GetError"](param);
        }
        if (!window.WebSocket) {
            return -1;
        };
        if (typeof (options) == "string") {//方法 
            if (!wsObject) {
                return 0;
            }
            if (!Methods[options]) {
                return 4;
            }
            Methods[options](param);
            return wsObject.readyState;
        } else {//属性或事件
            if (!wsObject && !options.url) {
                return 5;
            }
            wsOptions = options;
            wsObject = new WebSocket(options.url);
            wsObject.onopen = function (e) {
                if (!!options.onOpen) {
                    options.onOpen(e);
                }
            }
            wsObject.onmessage = function (e) {
                if (!!wsTimeout) {
                    clearTimeout(wsTimeout); //清空重连
                    wsTimeout = null;
                }
                if (!!options.onMessage) {
                    options.onMessage(e);
                }
            }
            wsObject.onerror = function (e) {
                if (!!options.onError) {
                    options.onError(e);
                }
            }
            wsObject.onclose = function (e) {
                if (!!options.onClose) {
                    options.onClose(e);
                }
                clearInterval(wsInterval); //清空心跳
                wsInterval = null;
                if (wsOptions.IsReConnect) {//是否重连，如果是手动断开，请调用WebSocktInterface("Close")                    
                    WebSocktInterface("ReConnect");
                }
            }
        }
        return wsObject.readyState;
    }
})();

export default WebSocktInterface